package controllers.Administrador;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import services.AdministradorService;
import controllers.AbstractController;
import domain.Administrador;
import forms.AdministradorForm;

@Controller
@RequestMapping("/security/registration")
public class RegisterAdministratorController extends AbstractController {

	@Autowired
	private AdministradorService administradorService;

	// Creation
	@RequestMapping(value = "/administrator", method = RequestMethod.GET)
	public ModelAndView registration() {
		ModelAndView result;
		AdministradorForm administratorForm;
		Boolean isAdministrator;

		isAdministrator = true;
		administratorForm = new AdministradorForm();

		result = new ModelAndView("security/registration/administrator");
		result.addObject("administratorForm", administratorForm);
		result.addObject("isAdministrator", isAdministrator);

		return result;
	}

	// Edition
	@RequestMapping(value = "/administrator", method = RequestMethod.POST, params = "save")
	public ModelAndView save(@Valid AdministradorForm administratorForm,
			BindingResult binding) {
		ModelAndView result;
		Administrador administrador;
		Boolean isAdministrator;

		if (binding.hasErrors()) {
			result = createEditModelAndView(administratorForm);
			result.addObject("erroresBindingAdmin", true);
			for (FieldError error : binding.getFieldErrors()) {
				if (error.getField().equals("email"))
					result.addObject("emailError", "security.error.email");
				if (error.getField().equals("password"))
					result.addObject("passwordError", "security.error.password");
				if (error.getField().equals("username"))
					result.addObject("usernameError", "security.error.username");
				if (error.getField().equals("surname"))
					result.addObject("surnameError", "security.error.surname");
				if (error.getField().equals("name"))
					result.addObject("nameError", "security.error.name");

			}

		} else {
			try {
				if (!administratorForm.getAcceptConditions()) {
					result = createEditModelAndView(administratorForm,
							"security.commit.noAcceptConditions");
				} else if (!administratorForm.getPassword().equals(
						administratorForm.getRepeatPassword())) {
					result = createEditModelAndView(administratorForm,
							"security.commit.passwordDifferents");
				} else {

					administrador = administradorService
							.reconstruct(administratorForm);

					administradorService.save(administrador);
					result = new ModelAndView("redirect:../../");
				}
			} catch (Throwable oops) {

				result = createEditModelAndView(administratorForm,
						"administrator.commit.error");
			}
		}
		isAdministrator = true;
		result.addObject("isAdministrator", isAdministrator);
		return result;
	}

	// Ancillary methods
	protected ModelAndView createEditModelAndView(
			AdministradorForm administratorForm) {
		ModelAndView result;

		result = createEditModelAndView(administratorForm, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(
			AdministradorForm administratorForm, String message) {
		ModelAndView result;

		result = new ModelAndView("security/registration/administrator");

		result.addObject("administratorForm", administratorForm);
		result.addObject("message", message);

		return result;
	}
}